package DMSXL._2024.DP;

public class canPartition {
    public static void main(String[] args) {

    }
    public boolean canPartition(int[] nums){
        int all = 0;
        for (int i = 0; i < nums.length; i++) {
            all += nums[i];
        }
        if(all % 2 != 0){
            return false;
        }
        //转换为0-1背包问题，物品的价值等于自身，去求容量为bagSize的背包最大价值是否为bagSize
        int bagSize = all /2;
        int[] dp = new int[bagSize + 1];
        for (int i = 0; i < nums.length; i++) {
            for (int j = bagSize; j >= nums[i] ; j--) {
                dp[j] = Math.max(dp[j],dp[j - nums[i]] + nums[i]);
            }
        }
        if(dp[bagSize] == bagSize){
            return true;
        }else {
            return false;
        }
    }
}
